(function () {
	
    //查询
    jQuery('.app-btn-search').on('click',function(){
        serverData.key=jQuery("#search").val();
        serverTable.draw();
    });
    //撤销查询
    jQuery('.app-btn-cancel').on('click',function(){
        jQuery('#search').val('');
        serverData.key='';
        serverTable.draw();
    });
    
    ///认证通过
    jQuery('.accessBtn').on('click', function () {
    	if (jQuery("#checkEditForm") && !jQuery("#checkEditForm").Validform({
			tiptype : 3,
			showAllError : true
		}).check(false)) {
			return;
		};
		    var id = jQuery('#id').val();
	        var url = window.rootPath + "/ui/BusinesssellerAuthentication/accessCheck";
	        var d;
	        if(id!=''){
	        	  d = {
	        			"id": id,
	     	            "memo": jQuery('#memo').val()
	     	        };
	        jQuery.ajax({
	            type: 'post',
	            url: encodeURI(url),
	            data: JSON.stringify(d),
	            dataType: 'json',
	            contentType: 'application/json;charset=utf-8',
	            success: function (data) {
	            	if(data.code==-1){
	            		message("认证失败！");
	            	}
	            	else{
	            		message("认证通过！")
	            	}
	            	jQuery('#checkModal').modal('hide');
	                serverTable.draw(false);
	            }
	        });
	        }
    });
    
  ///认证拒绝
    jQuery('.rejectBtn').on('click', function () {
    	if (jQuery("#checkEditForm") && !jQuery("#checkEditForm").Validform({
			tiptype : 3,
			showAllError : true
		}).check(false)) {
			return;
		};
		    var id = jQuery('#id').val();
	        var url = window.rootPath + "/ui/BusinesssellerAuthentication/rejectCheck";
	        var d;
	        if(id!=''){
	        	  d = {
	        			"id": id,
	     	            "memo": jQuery('#memo').val()
	     	        };
	        jQuery.ajax({
	            type: 'post',
	            url: encodeURI(url),
	            data: JSON.stringify(d),
	            dataType: 'json',
	            contentType: 'application/json;charset=utf-8',
	            success: function (data) {
	            	if(data.code==-1){
	            		message("拒绝失败！");
	            	}
	            	else{
	            		message("拒绝通过！")
	            	}
	            	jQuery('#checkModal').modal('hide');
	                serverTable.draw(false);
	            }
	        });
	        }
    });
$(".addBtn").on('click',function(){
	window.location = window.rootPath
	+ '/ui/eventRegister?eventReportIdentify='+$("#eventReportIdentify").val();
});
    var serverData = {};
    var serverTable;
    jQuery(document).ready(function () {
        serverTable = jQuery('#dataTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/eventRegisterDeal/getpage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [
                {
                data: null
                },
                {
                    data: null,
                    defaultContent: "",
                    render: function (data, type, full, meta) {
                    	
                    	var operate ='<button type="button" class="btn btn-sm btn-info detailBtn" title="详情"><i class="fa fa-edit"></i></button>';
//                    	if(data.registerEventState){
//                    		if(data.registerEventState.title=='初始化'){
//                        		operate+='<button type="button" class="btn btn-sm btn-info submitBtn" title="发布"><i class="fa fa-bullhorn"></i></button>';
//                        	}else if(data.registerEventState.title=='已分配'){
//                        		operate+='<button type="button" class="btn btn-sm btn-info backBtn" title="收回"><i class="fa fa-reply-all"></i></button>';
//                        	}else if(data.registerEventState.title=='已退回'){
//                        		operate+='<button type="button" class="btn btn-sm btn-info rejectBackBtn" title="确认退回"><i class="fa fa-recycle"></i></button>';
//                        	}
//                    	}
                    	
                        return operate;
                    }
                },
                {
                	data: 'registerEventName',
                    render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'registerEventContent',
                    render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'registerEventAccTime',
                    render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY/MM/DD HH:mm');
                        else
                            return '';
                    }
                },
                {
                    data: 'registerEventIdentify',
                    render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'registerEventType',
                    render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
                },
                {
                		data:'eventTaskTime',
                    render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY/MM/DD HH:mm');
                        else
                            return '';
                    }
                		
                },
                {
                		data:'eventTaskUserName'
                },
                {
                    data: 'gradingName',
                    render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'registerEventSeriousLevelTitle',
                    render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
                },{
					data : "registerEventImgAdress",
					render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return '<img width="50%" src="'+window.rootPath+"/"+data+'" />';
						}
					}

				},
                {
                    data: 'registerEventAdress',
                    render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'gridName',
                    render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'registerEventStateTitle',
                    render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
                }
              ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }

                serverTable.off('click', 'tr');
                serverTable.on('click', 'tr', function (event) {
                    event = event || window.event;
                    var target = event.target || event.scrElement;
                    var data = serverTable.row(this).data();
                    if (target.className.indexOf('fa-edit') != -1 ||target.className.indexOf('detailBtn') != -1) {
                        showDetail(data);
                    }
                    return;
                });
            }
        });
        var rejectBackBtnQ=function(o){
        	 $("#rejectBackId").val(o.id);
        	 
        	jQuery('#rejectBackModal').modal('show');
			$.ajax({
				url : rootPath + "/ui/eventRegister/getRejectBackReason?id="
						+ $("#rejectBackId").val(),
				dataType : 'json',
				type : 'get',
				contentType : "application/json",
				success : function(result) {
					if (result.data.code <= 0) {
						message("获取退回原因失败!");
					} else {
						if(result.data.reasonStr){
							$("#rejectBackReason").val(result.data.reasonStr.eventTaskMemo);
							if(result.data.reasonStr.eventTaskUser){
								$("#rejectBackPerson").val(result.data.reasonStr.eventTaskUser.name);
							}
						}
					}
				}
			});
        }
        $(".saverejectBackBtn").on(
        		'click',
        		function() {
        			var data = {
        				"id" : $("#rejectBackId").val(),
        				"rejectBackMemo" : $("#rejectBackMemo").val(),
        			}
        			$.ajax({
        				url : rootPath + "/ui/eventRegister/rejectBackEventRegister",
        				dataType : 'json',
        				data : JSON.stringify(data),
        				type : 'post',
        				contentType : "application/json",
        				success : function(result) {
        					if (result.data.code <= 0) {
        						message("操作失败!");
        					} else {
        						message(result.data.rejectBackStr);
        					}
        					jQuery('#rejectBackModal').modal('hide');
        					serverTable.draw(false);
        				}
        			});
        		});
        var backBtnQ=function(o){
        	jQuery('#backModal').modal('show');
        	$("#backId").val(o.id);
        };
        $(".savebackBtn").on(
        		'click',
        		function() {
        			var data = {
        				"id" : $("#backId").val(),
        				"backMemo" : $("#backMemo").val(),
        			}
        			$.ajax({
        				url : rootPath + "/ui/eventRegister/backEventRegister",
        				dataType : 'json',
        				data : JSON.stringify(data),
        				type : 'post',
        				contentType : "application/json",
        				success : function(result) {
        					if (result.data.code <= 0) {
        						message("操作失败!");
        					} else {
        						message(result.data.backStr);
        					}
        					jQuery('#backModal').modal('hide');
        					serverTable.draw(false);
        				}
        			});
        		});
        var submitBtnQ=function(o){
        	var data = {
    				"id" : o.id
    			}
    			$.ajax({
    				url : rootPath + "/ui/eventRegister/submitEventRegister",
    				dataType : 'json',
    				data : JSON.stringify(data),
    				type : 'post',
    				contentType : "application/json",
    				success : function(result) {
    					if (result.data.code <= 0) {
    						message("操作失败!");
    					} else {
    						message(result.data.submitStr);
    					}
    					serverTable.draw(false);
    				}
    			});
        }
        var showDetail=function(o){
        	if(!o) return;
        	window.location = window.rootPath
        	+ '/ui/eventRegister?eventReportIdentify='+o.registerEventToReportEvent+"&eventRegisterIdentify="+o.id;
        }
    });
    
    
    
    
    
    
})();